package com.bhs.sansonglogistics.utils;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.view.ViewGroup;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.target.CustomTarget;
import com.bumptech.glide.request.transition.Transition;
import com.luck.picture.lib.utils.PictureFileUtils;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

public class WatermarkUtils {


    /*
   添加全屏斜着45度的文字
   */
    public static void drawCenterLabel(String resource, ImageView imageView) {
        String text = "发货 找物流 就上 伞送app";
        Glide.with(imageView.getContext()).asBitmap().load(resource).into(new CustomTarget<Bitmap>() {
            @Override
            public void onResourceReady(@NonNull Bitmap bmp, @Nullable Transition<? super Bitmap> transition) {
                LogUtils.d(bmp.getWidth() + "===" + bmp.getHeight());
                //创建一样大小的图片
                Bitmap newBmp = Bitmap.createBitmap(bmp.getWidth(), bmp.getHeight(), Bitmap.Config.ARGB_8888);
                //创建画布
                Canvas canvas = new Canvas(newBmp);
                canvas.drawBitmap(bmp, 0, 0, null);  //绘制原始图片
                canvas.save();
                 canvas.rotate(-45,bmp.getWidth()/2,bmp.getHeight()/2); //顺时针转45度
                Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);

                paint.setColor(Color.argb(144, 220, 220, 220)); //白色半透明
                paint.setTextSize(bmp.getWidth() / 30f);
                paint.setDither(true);
                paint.setFilterBitmap(true);
                Rect rectText = new Rect();  //得到text占用宽高， 单位：像素
                paint.getTextBounds(text, 0, text.length(), rectText);
                for (int i = 0; i < 6; i++) {
                    if((i % 2) == 1) {
                        canvas.drawText(text, bmp.getWidth()/3, bmp.getHeight() / 5 * i, paint);
                    }else {
                        canvas.drawText(text, 0, bmp.getHeight() / 5 * i, paint);
                    }
                }
//
                canvas.restore();
                imageView.setImageBitmap(newBmp);
            }

            @Override
            public void onLoadCleared(@Nullable Drawable placeholder) {

            }
        });
    }

    /***
     * 添加图片水印
     * @param watermark 水印图像
     * @return
     */
    private static void createWaterMarkBitmap(String resource, Bitmap watermark, ImageView imageView) {
        Glide.with(imageView.getContext()).asBitmap().load(resource).into(new CustomTarget<Bitmap>() {
            @Override
            public void onResourceReady(@NonNull Bitmap srcBitmap, @Nullable Transition<? super Bitmap> transition) {
                int srcWidth = srcBitmap.getWidth();//获取源图像的宽
                int srcHeight = srcBitmap.getHeight();//获取源图像的高
                int waterWidth = watermark.getWidth();//获取水印图像的宽
                int waterHeight = watermark.getHeight();//获取水印图像的高

                //int proportion= waterWidth / srcWidth; //得到宽初始比例

                int proportionWidth = (int) (srcWidth * 0.16); //计算后宽的比例值
                int proportionHeight = ((proportionWidth * 100 / waterWidth)) * waterHeight / 100;//计算后高的比例值

                //返回根据比例缩放之后bitmap
                Bitmap newBitmap = getNewBitmap(watermark, proportionWidth, proportionHeight);

                Bitmap bitmap = Bitmap.createBitmap(srcWidth, srcHeight, Bitmap.Config.ARGB_8888);//创建一个空白位图
                Canvas canvas = new Canvas(bitmap);//根据位图创建画布
                canvas.drawBitmap(srcBitmap, 0, 0, null);//先画出源图像
                canvas.drawBitmap(newBitmap, srcWidth - newBitmap.getWidth() - (newBitmap.getWidth() / 4), srcHeight - newBitmap.getHeight() - (newBitmap.getHeight() / 5), null);//然后在画水印图像
                imageView.setImageBitmap(bitmap);
            }

            @Override
            public void onLoadCleared(@Nullable Drawable placeholder) {

            }
        });

    }

    /**
     * 代码修改bitmap的宽高
     *
     * @param bitmap
     * @param newWidth
     * @param newHeight
     * @return
     */
    public static Bitmap getNewBitmap(Bitmap bitmap, int newWidth, int newHeight) {
        // 获得图片的宽高.
        int width = bitmap.getWidth();
        int height = bitmap.getHeight();
        // 计算缩放比例.
        float scaleWidth = ((float) newWidth) / width;
        float scaleHeight = ((float) newHeight) / height;
        // 取得想要缩放的matrix参数.
        Matrix matrix = new Matrix();
        matrix.postScale(scaleWidth, scaleHeight);
        // 得到新的图片.
        Bitmap newBitmap = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true);
        return newBitmap;
    }
}
